package com.demo.controller;

import com.demo.domain.User;
import com.demo.service.IRegService;
import com.sun.javafx.image.PixelUtils;
import com.sun.org.apache.xpath.internal.operations.String;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Created by duanxifeng on 2017/5/12.
 */

@RestController
@EnableAutoConfiguration
@RequestMapping("/HelloWorld")
public class HelloWorldController {

    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());


    public void AddUser()
    {
        User user1=new User();
        user1.setUserId("11111111111");
        user1.setUserName("张三");
        user1.setPwd("22222");
        user1.setAge("20");
        user1.setName("李四");
        users.put(Long.parseLong("1") ,user1);
    }

    @Autowired
    private IRegService regService;

    @RequestMapping("/hello1")
    java.lang.String home(){
        return "index";
    }


    @RequestMapping("/hello2")
    public java.lang.String Index(ModelMap map){
        map.addAttribute("name","这是第一个张三");
        map.addAttribute("date", LocalDateTime.now());
        return "Index";
    }



//    @RequestMapping(value="/",method = RequestMethod.POST)
//    public List<User> getUserlist()
//    {
//        List<User> r=new ArrayList<User>(users.values());
//        return r;
//    }

    @RequestMapping(value="/",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取用户",notes = "")
    public java.lang.String postUser(@ModelAttribute User user){
        users.put(user.getId(),user);
        return "Success";
    }

    @RequestMapping(value = "/{id}",method =RequestMethod.GET)
    @ResponseBody
    public User getUser(@PathVariable Long id){

        AddUser();
        return users.get(id);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "新增用户",notes = "根据User创建用户")
    @ApiImplicitParam(name = "User",value = "创建用户",required = true,dataType = "User")
    public java.lang.String putUser(@PathVariable Long id,@ModelAttribute User user){
        User u=users.get(id);
        u.setUserName(user.getUserName());
        u.setPwd(user.getPwd());
        u.setUserId(user.getUserId());
        users.put(id,u);
        return  "Success";
    }


    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public java.lang.String deleteUser(@PathVariable Long id){
        users.remove(id);
        return "success";
    }


    @RequestMapping("/reg")
    boolean reg(@RequestParam("loginPwd") String loginNum,@RequestParam("userId") String userId){
        return  false;
    }

    private java.lang.String creatMD5(java.lang.String loginNum){
        // 生成一个MD5加密计算摘要
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(loginNum.getBytes());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new BigInteger(1, md.digest()).toString();
    }
}
